/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/25
 * Time: 上午7:02
 */
$(function () {
    sendAjax('/v1/hro/teacher/detail.json', 'get', {id:id}, function(json) {
        var teacher = json.body.data;
        $('.teacher-realname').text(teacher.realname);
        $('.teacher-number').text(teacher.number);
        var gender = teacher.gender == 1 ? '男' : '女';
        $('.teacher-gender').text(gender);
        $('.teacher-identity').text(teacher.identity);
        $('.teacher-mobile').text(teacher.mobile);
        $('.teacher-unit').text(teacher.unit_name);
        $('.teacher-join-at').text(teacher.join_at);
        $('.teacher-work-age').text(teacher.work_age+'年');
        $('.teacher-education').text(teacher.education);
        if (teacher.education_profile) {
            $('.education-profile').attr('target', '_blank').attr('href', '/pictures/'+teacher.education_profile);
        }
        if (teacher.degree_profile) {
            $('.degree-profile').attr('target', '_blank').attr('href', '/pictures/'+teacher.degree_profile);
        }
        $('.teacher-job').text(teacher.job);
        $('.teacher-professional-title').text(teacher.professional_title);
        $('.teacher-professional-check').text(teacher.professional_check);
        if (teacher.professional_title_profile) {
            $('.professional-title-profile').attr('target', '_blank').attr('href', '/pictures/'+teacher.professional_title_profile);
        }
        $('.teacher-duty-on').text(teacher.duty_on);
        $('.teacher-duty-type').text(teacher.duty_type);
        $('.teacher-capacity').text(teacher.capacity);
        var marriage = teacher.marriage == 2 ? '已婚' : '未婚';
        $('.teacher-marriage').text(marriage);
        if (teacher.marriage_profile) {
            $('.marriage-profile').attr('target', '_blank').attr('href', '/pictures/'+teacher.marriage_profile);
        }
        $('.teacher-spouse-name').text(teacher.spouse_name);
        $('.teacher-spouse-unit').text(teacher.spouse_unit);
        var has_house = teacher.has_house == 1 ? '有' : '无';
        $('.teacher-has-house').text(has_house);
        if (teacher.house_profile) {
            $('.house-profile').attr('target', '_blank').attr('href', '/pictures/'+teacher.house_profile);
        }
        $('.sum-score').text(teacher.score);
        sendAjax('/v1/hro/teacher/score/list.json', 'get', {id:id}, function(json) {
            var scores = json.body.rows;
            $.each(scores, function(n, v) {
                $(getKeyClass(v.key)).parent().find('.check-time').text(v.updated_at);
                $(getKeyClass(v.key)).parent().find('.check-remark').text(v.remark);
                $(getKeyClass(v.key)).parent().find('label a').attr('data-remark', v.remark);
                $(getKeyClass(v.key)).parent().find('.teacher-score').text(v.score);
                $(getKeyClass(v.key)).parent().find('label a').attr('data-score', v.score);
            });
        });
    });
    $('#view-score-dialog').on('show.bs.modal', function(event) {
        var button = $(event.relatedTarget);
        var key = button.data('key');
        var score = button.data('score');
        var remark = button.data('remark');
        $(this).find('.score-key').val(key);
        $(this).find('.score-num').val(score);
        $(this).find('.score-remark').val(remark);
    });
    $('#view-score-dialog').on('hide.bs.modal', function() {
        $(this).find('.score-key').val('');
        $(this).find('.score-num').val('');
        $(this).find('.score-remark').val('');
    });
    $('#view-score-dialog .btn-save').on('click', function() {
        var data = {
            teacher_id : $('#view-score-dialog').find('.teacher-id').val(),
            key : $('#view-score-dialog').find('.score-key').val(),
            score : $('#view-score-dialog').find('.score-num').val(),
            remark : $('#view-score-dialog').find('.score-remark').val()
        };
        sendAjax('/v1/hro/teacher/score/save', 'post', {data:data}, function(json) {
            $('#action-tip').find('.tip-content').text(json.message);
            $('#view-score-dialog').modal('hide');
            $('#action-tip').modal('show');
        });
    });
    $('#action-tip').on('hide.bs.modal', function() {
        location.reload();
    });
});

function getKeyClass(key) {
    switch (key) {
        case 4: return '.teacher-join-at';
        case 5: return '.teacher-work-age';
        case 6: return '.teacher-education';
        case 7: return '.teacher-job';
        case 8: return '.teacher-professional-title';
        case 9: return '.teacher-marriage';
        case 10: return '.teacher-spouse-name';
        case 11: return '.teacher-spouse-unit';
        case 12: return '.teacher-has-house';
    }
}